﻿/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
 * Description:
 * Author: 辛爱国 x30005137
 * Create: 2020-07-28
 * Notes: 无
 */
#pragma execution_character_set("utf-8")

#include <QGraphicsDropShadowEffect>
#include <QDebug>
#include <QScreen>
#include <QAbstractItemView>
#include <QListView>
#include <qapplication.h>
#include <QDesktopWidget>
#include <qscreen.h>
#include "settingdialog.h"
#include "reconfirmation.h"
#include "IShareServiceController.h"
#include <QtAlgorithms>

using namespace Huawei;
using namespace Idea;

SettingDialog::SettingDialog(QWidget *parent) : HWDialog(parent)
{
    setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
    QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
    shadow_effect->setOffset(0, 0);
    shadow_effect->setColor(Qt::gray);
    shadow_effect->setBlurRadius(10); // 设置阴影10
    this->widgetWindow->setGraphicsEffect(shadow_effect);
    init();
#if defined(IDEASHAREKEY)
    this->labelRestart->hide();
    initKey();
#endif
    saveChildrenInfo();
}
#if defined(IDEASHAREKEY)
void SettingDialog::initKey()
{
    this->labelCloseButton->setVisible(false);
    this->labelDigits->setVisible(false);
    this->labelAutoHide->setVisible(false);
    this->checkBoxAutoHide->setVisible(false);
    this->widgetDigits->setVisible(false);
    this->widgetClose->setVisible(false);

    QPoint aimPt = this->labelCloseButton->pos();
    int moveUp = this->labelLanguage->pos().y() - aimPt.y();
    this->labelLanguage->move(aimPt);

    aimPt = this->comboBoxLanguage->pos();
    aimPt.setY(aimPt.y() - moveUp);
    this->comboBoxLanguage->move(aimPt);

    aimPt = this->labelRestart->pos();
    aimPt.setY(aimPt.y() - moveUp);
    this->labelRestart->move(aimPt);

    moveUp = 130; //  整体上移130
    aimPt = this->labelShareToScreen->pos();
    aimPt.setY(aimPt.y() - moveUp);
    this->labelShareToScreen->move(aimPt);

    aimPt = this->pushButtonLeftScreen->pos();
    aimPt.setY(aimPt.y() - moveUp);
    this->pushButtonLeftScreen->move(aimPt);

    aimPt = this->pushButtonRightScreen->pos();
    aimPt.setY(aimPt.y() - moveUp);
    this->pushButtonRightScreen->move(aimPt);

    aimPt = this->pushButtonOK->pos();
    aimPt.setY(aimPt.y() - moveUp);
    this->pushButtonOK->move(aimPt);

    aimPt = this->pushButtonCancel->pos();
    aimPt.setY(aimPt.y() - moveUp);
    this->pushButtonCancel->move(aimPt);

    QRect rect = this->widgetBody->geometry();
    rect.setHeight(rect.height() - moveUp);
    this->widgetBody->setGeometry(rect);

    rect = this->widgetWindow->geometry();
    rect.setHeight(rect.height() - moveUp);
    this->widgetWindow->setGeometry(rect);

    rect = this->geometry();
    rect.setHeight(rect.height() - moveUp);
    this->setGeometry(rect);

    QString str = this->widgetBody->styleSheet();
    this->widgetWindow->setStyleSheet("QWidget{border-radius: 6px;}");
    if (str == "") {
    };
}
#endif
void SettingDialog::resizeWindow()
{
    HWDialog::resizeWindow();
    setBuleBoder();
}

void SettingDialog::init()
{
    /* 投屏是否自动隐藏 */
    //this->checkBoxAutoHide->setChecked(ideasharesdk::config::autohide.value());
    QAbstractItemView *view = new QListView(this->comboBoxLanguage);
    this->comboBoxLanguage->setView(view);
    QWidget *parents = view->parentWidget();
    parents->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    view->setParent(parents);

    // 超八位隐藏
    this->radioButtonDigitsOverEight->hide();
    this->radioButtonDigitsEightOrSix->hide();
    this->labelDigits->hide();

    QPoint curPoint = this->labelAutoHide->pos();
    this->labelAutoHide->move(QPoint(0, -13) + curPoint);    // 移动相对位移QPoint(0, -13)
    curPoint = this->checkBoxAutoHide->pos();
    this->checkBoxAutoHide->move(QPoint(0, -13) + curPoint); // 移动相对位移QPoint(0, -13)

    curPoint = this->labelLanguage->pos();
    this->labelLanguage->move(QPoint(0, 13) + curPoint);     // 移动相对位移QPoint(0, 13)
    curPoint = this->comboBoxLanguage->pos();
    this->comboBoxLanguage->move(QPoint(0, 13) + curPoint);  // 移动相对位移QPoint(0, 13)
    curPoint = this->labelRestart->pos();
    this->labelRestart->move(QPoint(0, 13) + curPoint);      // 移动相对位移QPoint(0, 13)
}
SettingDialog::~SettingDialog() {}
void SettingDialog::setBuleBoder()
{
    if (chooseShareScreen == SCREEN_PRIMARY) {
        pushButtonRightScreen->setStyleSheet("QPushButton{border:3px solid #B7B7B7;}");
        pushButtonLeftScreen->setStyleSheet("QPushButton{border:3px solid #0D94FF;}");
    } else {
        pushButtonLeftScreen->setStyleSheet("QPushButton{border:3px solid #B7B7B7;}");
        pushButtonRightScreen->setStyleSheet("QPushButton{border:3px solid #0D94FF;}");
    }
}
void SettingDialog::screenShot()
{
    /* 初始化屏幕选择屏幕截图 */
    chooseShareScreen = SCREEN_PRIMARY;
    QList<QScreen *> screenList = QGuiApplication::screens();
    int screenCount = screenList.size();
    pushButtonRightScreen->setVisible(screenCount > 1 ? true : false);

    constexpr int width_4k = 3840;
    constexpr int height_4k = 2160;
    constexpr double pixel_4k = width_4k * height_4k;
    for (int i = 0; i < screenCount; i++) {
        if (screenList[i] == nullptr) continue;
        QRect screenGeometry = screenList[i]->geometry();
        int x = 0;
        int y = 0;
        int rheight = screenGeometry.height();
        int rwidth = screenGeometry.width();
        double rpixel = rheight * rwidth;
        if (rpixel > pixel_4k) {
            rheight = height_4k * (pixel_4k / rpixel);
            rwidth = width_4k * (pixel_4k / rpixel);
            x = x + (width_4k - rwidth) / 2;    // 左右 2 处分别预留位置
            y = y + (height_4k - rheight) / 2;  // 上下 2 处分别预留位置
        }
        try {
            QPixmap originalPixmap = screenList[i]->grabWindow(0, x, y, rwidth, rheight);
            QSize size = QSize(128, 72); // 128宽72高
            if (i == 0) {
                this->pushButtonLeftScreen->setIcon(QIcon(originalPixmap));
                this->pushButtonLeftScreen->setIconSize(size * getCurrentRate(this->parentWidget()));
                this->pushButtonLeftScreen->setProperty("ICONSIZE", size);
            }
            if (i == 1) {
                this->pushButtonRightScreen->setIcon(QIcon(originalPixmap));
                this->pushButtonRightScreen->setIconSize(size * getCurrentRate(this->parentWidget()));
                this->pushButtonRightScreen->setProperty("ICONSIZE", size);
            }
        } catch (std::exception &e) {
            qDebug() << "#### EXCEPTION : " << e.what() << endl;
        }
        qDebug() << screenList[i]->name();
    }
    setBuleBoder();
}

void SettingDialog::on_pushButtonOK_clicked()
{
#if defined(IDEASHAREKEY)
    if (ideasharesdk::config::language != (ideasharesdk::IDEASHARE_LANGUAGE)(this->comboBoxLanguage->currentIndex() + 1)) {
        ReConfirmation rec(this);
        rec.SetTex(QObject::tr("The smart projector will take effect when it is inserted into the computer next time."));
        rec.move(geometry().center() - rec.rect().center());
        rec.resizeWindow();
        rec.move(geometry().center() - rec.rect().center());
        if (rec.exec()) {
            ideasharesdk::config::language = (ideasharesdk::IDEASHARE_LANGUAGE)(this->comboBoxLanguage->currentIndex() + 1);
        } else {
            this->comboBoxLanguage->setCurrentIndex(ideasharesdk::config::language.value() - 1);
            return;
        }
    }
    //ideasharesdk::config::sharescreen = this->chooseShareScreen;
    hide();
#else
    mEuaIP = this->textEuaIp->toPlainText().trimmed();
    QRegExp regPort("^\\d+$");
    QString euaPort = this->euaPortEdit->toPlainText().trimmed();
    if (!regPort.exactMatch(euaPort)) {
        qDebug() << "not the port...";
        mEuaPort = 0;
    } else {
        mEuaPort = euaPort.toUInt();
    }
    QString logLevel = this->textLogLevel->toPlainText().trimmed();
    if (!regPort.exactMatch(logLevel)) {
        qDebug() << "default log level";
        mLogLevel = 3;
    } else {
        mLogLevel = logLevel.toUInt();
        qDebug() << "set the log level: " << mLogLevel <<endl;
    }
    QString mouseShape = this->textMouseShape->toPlainText().trimmed();
    if (!regPort.exactMatch(mouseShape)) {
        qDebug() << "default mouseShape";
        mMouseShape = 255;
    } else {
        mMouseShape = mouseShape.toUInt();
        qDebug() << "qstring mouseShape : " << mouseShape << endl;
        qDebug() << "qstring mouseShape :set the mouse shape: " << mMouseShape << endl;
    }
    
    ShareServiceController* contorller = ShareServiceController::GetInstance();
    if (contorller != NULL) {
        qDebug() << "Set Choosed Screen: " << this->chooseShareScreen;
        contorller->SetDisplayScreen(this->chooseShareScreen);
        // 重新初始化日志
        const LogInfo logInfo = { "log\\",(LogLevel)mLogLevel,true };
        contorller->SetLog(logInfo);
        qDebug() << "set log level2: " << mLogLevel;

        // 设置鼠标样式
        contorller->SetMouseShape((ShareMouseShape)mMouseShape);
    }

    hide();
#endif
    qDebug() << "Choosed Screen: " << this->chooseShareScreen;
}

void SettingDialog::on_pushButtonCancel_clicked()
{
    hide();
}
void SettingDialog::on_pushButtonExit_clicked()
{
    hide();
}

void SettingDialog::on_pushButtonLeftScreen_clicked()
{
    chooseShareScreen = SCREEN_PRIMARY;
    pushButtonRightScreen->setStyleSheet("QPushButton{border:3px solid #B7B7B7;}");
    pushButtonLeftScreen->setStyleSheet("QPushButton{border:3px solid #0D94FF;}");
}
void SettingDialog::on_pushButtonRightScreen_clicked()
{
    chooseShareScreen = SCREEN_SECONDARY;
    pushButtonLeftScreen->setStyleSheet("QPushButton{border:3px solid #B7B7B7;}");
    pushButtonRightScreen->setStyleSheet("QPushButton{border:3px solid #0D94FF;}");
}

QString SettingDialog::getEuaIP()
{
    return mEuaIP;
}
uint32_t SettingDialog::getEuaPort()
{
    return mEuaPort;
}
uint32_t SettingDialog::getLogLevel()
{
    return mLogLevel;
}
uint32_t SettingDialog::getMouseShape()
{
    return mMouseShape;
}
QString SettingDialog::mEuaIP = {};
uint32_t SettingDialog::mEuaPort = 0;
uint32_t SettingDialog::mLogLevel = 3; /* 默认设置为debug */
uint32_t SettingDialog::mMouseShape = 255; /* 激光笔，默认颜色 */